//
//  GTWeChatDynamicTagView.m
//  GTW
//
//  Created by Ran on 15/11/12.
//  Copyright (c) 2015年 xcode. All rights reserved.
//

#import "GTWeChatDynamicTagView.h"
#import "GTWeChatDynamicItemModel.h"
#import "RanTestModel.h"
#import "GTWeChatDynamicTuple.h"
#import "GTWeChatDynamicTool.h"
#import "GTWeChatDynamicSettings.h"
#import <TYAttributedLabel/TYAttributedLabel.h>
#import "UIButton+RanSet.h"

#pragma mark - ------------------------Model------------------------

@interface GTWeChatDynamicTagViewModel: NSObject

@property(nonatomic, strong)RanTestModel *sourceModel;
@property(nonatomic, assign)CGFloat theWidth;

@property(nonatomic, assign)CGRect scrollFrame;
@property(nonatomic, assign)CGSize scrollContentSize;
@property(nonatomic, assign)CGRect tagLabelFrame;

@property(nonatomic, strong)TYTextContainer *tagTextContainer;

@end

@implementation GTWeChatDynamicTagViewModel

@end

#pragma mark - ------------------------View------------------------

@interface GTWeChatDynamicTagView()

@property(nonatomic, strong)UIScrollView *scrollview;
@property(nonatomic, strong)TYAttributedLabel *tagLabel;

@end

@implementation GTWeChatDynamicTagView

#pragma mark - Protocol

+ (GTWeChatDynamicTuple *)getProcessedDataFromSourceData:(id)sourceData withWidth:(CGFloat)theWidth
{
    GTWeChatDynamicTagViewModel *model = [[GTWeChatDynamicTagViewModel alloc] init];
    model.sourceModel = (RanTestModel *)sourceData;
    CGFloat height;
    __block CGFloat textWidth;
    TYTextContainer *container;
    CGFloat tagLeftSpacing = 12.0;
    CGFloat tagTopSpacing = 5.0;
    
    if (model.sourceModel.tagOnes.count) {
        
        container = [[TYTextContainer alloc] init];
        NSDictionary *attributes = [GTWeChatDynamicSettings tagTextAttributes];
        NSAttributedString *space = [[NSAttributedString alloc] initWithString:@"  " attributes:attributes];
        CGFloat spaceWidth = [space boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.width;
        textWidth = 0;
        
        [model.sourceModel.tagOnes enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) {
            if (idx) {
                [container appendTextAttributedString:space];
                textWidth += spaceWidth;
            }
            NSString *tag = dic[@"name"];
            UIButton *button = [GTWeChatDynamicTool buttonWithAttribute:attributes];
            CGSize textSize = [GTWeChatDynamicTool getTextSizeWithText:tag attribute:attributes];
            button.frame = CGRectMake(0, 0, textSize.width + tagLeftSpacing * 2, textSize.height + 2 * tagTopSpacing);
            button.text = tag;
            button.backgroundColor = [UIColor lightGrayColor];
            [container appendView:button];
            textWidth += button.frame.size.width;
        }];
        
        container = [container createTextContainerWithTextWidth:MAXFLOAT];
        height = container.textHeight;
        
    }else{
        height = 0;
        textWidth = 0;
    }
    
    
    CGFloat scrollX = 0;
    CGFloat scrollY = 0;
    CGFloat scrollWidth = theWidth;
    CGFloat scrollHeight = height;
    model.scrollFrame = CGRectMake(scrollX, scrollY, scrollWidth, scrollHeight);
    model.scrollContentSize = CGSizeMake(textWidth, height);
    
    CGFloat labelX = 0;
    CGFloat labelY = 0;
    CGFloat labelWidth = textWidth;
    CGFloat labelHeight = height;
    model.tagLabelFrame = CGRectMake(labelX, labelY, labelWidth, labelHeight);
    
    model.tagTextContainer = container;
    
    GTWeChatDynamicTuple *tuple = [[GTWeChatDynamicTuple alloc] init];
    tuple.height = height;
    tuple.data = model;
    return tuple;
}

- (void)feedbackStoreData:(id)data
{
    GTWeChatDynamicTagViewModel *model = (GTWeChatDynamicTagViewModel *)data;
    self.tagLabel.textContainer = model.tagTextContainer;
    self.tagLabel.frame = model.tagLabelFrame;
    self.scrollview.frame = model.scrollFrame;
    self.scrollview.contentSize = model.scrollContentSize;
}

#pragma mark - Getter

- (UIScrollView *)scrollview
{
    if (!_scrollview) {
        self.scrollview = ({
            UIScrollView *scrollview = [[UIScrollView alloc] init];
            scrollview.showsHorizontalScrollIndicator = NO;
            scrollview.showsVerticalScrollIndicator = NO;
            [self addSubview:scrollview];
            scrollview;
        });
    }
    return _scrollview;
}

- (TYAttributedLabel *)tagLabel
{
    if (!_tagLabel) {
        self.tagLabel = ({
            TYAttributedLabel *tagLabel = [[TYAttributedLabel alloc] init];
            [self.scrollview addSubview:tagLabel];
            tagLabel;
        });
    }
    return _tagLabel;
}

@end
